package org.example.config.sequence.inspect;

import java.util.ArrayList;
import java.util.List;

public class Inspector {
    private static final List<String> logs = new ArrayList<>();
    private static long threadId;
    private static String threadName;

    public static void log(Class<?> clazz, String msg) {
        check();
        logs.add("[" + clazz.getSimpleName() + "]\t" + msg);
    }

    public static void constructor(Class<?> clazz) {
        check();
        logs.add("[" + clazz.getSimpleName() + "]\tconstructor creating");
    }


    private static void check() {
        if (threadId == 0) {
            threadId = Thread.currentThread().getId();
            threadName = Thread.currentThread().getName();
        } else if (threadId != Thread.currentThread().getId()) {
            throw new RuntimeException("thread changed! " + threadName + " -> " + Thread.currentThread().getName());
        }
    }

    public static void print() {
        System.out.println("==================");
        System.out.println("------------------");
        for (int i = 0; i < logs.size(); i++) {
            System.out.println((i + 1) + ".\t" + logs.get(i));
        }
//        logs.stream().forEach(System.out::println);
        System.out.println("------------------");
        System.out.println("==================");
    }

}
